package com.hnevc.moshuizhu1;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class PinglunActivity extends AppCompatActivity {

    private EditText pl;
    private Button button;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pinglun_main);
        initView();
        initEvent();
    }

    private void initView() {
        pl = findViewById(R.id.pinglun);
        button = findViewById(R.id.bu);
    }

    private void initData() {
        String url = "http://10.1.62.39/api/comment/add";
        String pinglu = pl.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("foodid","0462");
            jsonObject.put("userid","莫水珠");
            jsonObject.put("comment",pinglu);
            jsonObject.put("postdate", "2021-06-30 00:00:00");

        }catch (JSONException e){
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(PinglunActivity.this,jsonObject.optString("msg")+"",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(PinglunActivity.this,"网络请求失败!",Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(PinglunActivity.this);
        queue.add(jsonObjectRequest);
    }

    private void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }
}